import requests
import json
import os

tab_list = [
    {"title": "热搜", "tab": "realtime"},
    {"title": "小说", "tab": "novel"},
    {"title": "电影", "tab": "movie"},
    {"title": "电视剧", "tab": "teleplay"},
    {"title": "汽车", "tab": "car"},
    {"title": "游戏", "tab": "game"},
]

def write_to_json(data, filename):
    os.makedirs(os.path.dirname(filename), exist_ok=True)
    with open(filename, "w", encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False, indent=4)
    print(f"数据已成功写入到 {filename}")


def get_list(tab, title):
    print(f"开始获取 {title}...")
    url = f"https://example.com/api/board?tab={tab}"  # 替换为实际 API 接口
    headers = {"User-Agent": "Mozilla/5.0"}
    response = requests.get(url, headers=headers)
    data = response.json()  # 假设返回 JSON 数据

    # 处理数据
    data_list = [{"title": item["name"], "link": item["url"]} for item in data["data"]]
    filename = f"output/api/{title}.json"
    write_to_json(data_list, filename)
    print(f"完成获取 {title}. 一共获取到 {len(data_list)} 条数据。")
    print("--------------------------------------------")


for item in tab_list:
    get_list(item["tab"], item["title"])
